import random

WIDTH = 400
HEIGHT = 400

BACK_COLOR = (30, 30, 30)

p_s=0
AI_s=0
class MyRect:
    def __init__(self, x, y, w, h, clr):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.clr = clr
        self.speed = 2
    def SetSpeed(self, speed):
        self.speed = speed

    def Update(self):
        pass

    def GetGridPos(self):
        gx = self.x // self.w
        gy = self.y // self.h

        return gx, gy
    def Draw(self):
        rc = Rect((self.x - self.w // 2, self.y - self.h // 2), (self.w, self.h))
        screen.draw.filled_rect(rc, self.clr)

rc_list = []
unit = 50
clr_list = [
        (0, 180, 0),
        (180, 0, 0),
        (0, 0, 180),
        (0, 180, 180),
        (180, 0, 180),
        (180, 180, 0),
]
def GetMaxPoint():
    Max=0
    Max_xy=[-1,-1]
    for rc in rc_list:
        score=0
        if rc.clr==clr_list[0]:
            score=3
        elif rc.clr==clr_list[1]:
            score=5
        elif rc.clr==clr_list[2]:
            score=1
        if score>=Max:
            Max=score
            Max_xy[0]=rc.x//unit
            Max_xy[1]=rc.y//unit
    return Max_xy
def win():
    global p_s
    global AI_s
    if p_s>AI_s:
        screen.draw.text("Player Win!",center=(200,200))
    elif p_s==AI_s:
        screen.draw.text("Draw!",center=(200,200))
    else:
        screen.draw.text("AI Win!",center=(200,200))
    screen.draw.text("Use space to start a new turn",center=(200,390))
def reset():
    global clr_list
    for r in range(0, (WIDTH//unit)):
        for c in range(0, (HEIGHT//unit)):
            clr_index=random.randint(0,2)
            clr = clr_list[clr_index]
            rc = MyRect(r * unit + unit // 2, c * unit + unit // 2, unit, unit, clr)
            rc_list.append(rc)

reset()

def on_key_down(key):
    global rc_list
    global p_s
    global AI_s
    global WIDTH
    if key == keys.ESCAPE:
        quit()
    elif key == keys.SPACE and len(rc_list)==0:
        p_s=0
        AI_s=0
        reset()
    elif key == keys.R:
        WIDTH=500
    elif key == keys.C:
        WIDTH=400
def on_mouse_down(pos):
    global p_s
    global clr_list
    global AI_s
    global rc_list
    print("mouse_down", pos)
    x = pos[0]
    y = pos[1]
    gx = x // unit
    gy = y // unit
    print(gx, gy)

    for rc in rc_list:
        rcgx, rcgy = rc.GetGridPos()
        #print(rcgx, rcgy)
        if rcgx == gx and rcgy == gy:
            if rc.clr==clr_list[0]:
               p_s+=3
            elif rc.clr==clr_list[1]:
                p_s+=5
            else:
                p_s+=1
            rc_list.remove(rc)
            print(len(rc_list))
            if(len(rc_list)==0):
                return
            break
    if(random.randint(0,10)<=2):
        print("super")
        n=GetMaxPoint()
        gx=n[0]
        gy=n[1]
        print("super")
    else:
        flag=True
        flag2=-1
        while flag and flag2<10000:
            flag2+=1
            gx=random.randint(0,5)
            gy=random.randint(0,5)
            for rc in rc_list:
                rcgx, rcgy = rc.GetGridPos()
                if rcgx == gx and rcgy == gy:
                    flag=False
                    break
    for rc in rc_list:
        rcgx, rcgy = rc.GetGridPos()
        #print(rcgx, rcgy)
        if rcgx == gx and rcgy == gy:
            if rc.clr==clr_list[0]:
               AI_s+=3
            elif rc.clr==clr_list[1]:
                AI_s+=5
            else:
                AI_s+=1
            rc_list.remove(rc)
            print(len(rc_list))
            break

def on_mouse_up(pos):
    print("mouse_up", pos)

def update():
    pass

def draw():
    global p_s
    global AI_s
    global rc_list
    screen.clear()
    screen.fill(BACK_COLOR)
    if(len(rc_list)==0):
        win()
    for rc in rc_list:
        rc.Draw()
    screen.draw.text("Player:"+str(p_s),center=(200,10))
    screen.draw.text("AI:"+str(AI_s),center=(200,30))
    screen.draw.text("RULE\nclick blocks,\nif it is red,you\ncan get 5 poi\nnt.if its green\nyou can get 3\npoint."+
    "elif it is\nblue,you can\nget 1 point. y\nou will chess\nwith AI.",
    center=(450,110))
